﻿using Newtonsoft.Json.Linq;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Text;

namespace NP.BPMReportPlatform.Utilities
{
    public static class ConvertExtension
    {
        public static int? ObjToNullInt(this object thisValue)
        {
            if (thisValue == null)
            {
                return null;
            }

            if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out int result))
            {
                return result;
            }

            return null;
        }

        public static decimal? ObjToNullDecimal(this object thisValue)
        {
            if (thisValue == null)
            {
                return null;
            }

            if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out decimal result))
            {
                return result;
            }

            return null;
        }

        public static DateTime? ObjToNullDate(this object thisValue)
        {
            if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out DateTime result))
            {
                return result;
            }
            else
            {
                return null;
            }
        }

        public static bool? ObjToNullBool(this object thisValue)
        {
            if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out bool result))
            {
                return result;
            }
            else
            {
                return null;
            }
        }

        public static long? ObjToNullLong(this object thisValue)
        {
            if (thisValue != null && thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out long result))
            {
                return result;
            }
            else
            {
                return null;
            }
        }

        public static int? ObjToNullInt(this object thisValue, object errorValue)
        {
            if (thisValue == null)
            {
                return ObjToNullInt(errorValue);
            }

            if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out int result))
            {
                return result;
            }

            return ObjToNullInt(errorValue);
        }

        public static decimal? ObjToNullDecimal(this object thisValue, object errorValue)
        {
            if (thisValue == null)
            {
                return ObjToNullDecimal(errorValue);
            }

            if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out decimal result))
            {
                return result;
            }

            return ObjToNullDecimal(errorValue);
        }

        public static DateTime? ObjToNullDate(this object thisValue, object errorValue)
        {
            if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out DateTime result))
            {
                return result;
            }
            else
            {
                return ObjToNullDate(errorValue);
            }
        }

        public static bool? ObjToNullBool(this object thisValue, object errorValue)
        {
            if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out bool result))
            {
                return result;
            }
            else
            {
                return ObjToNullBool(errorValue);
            }
        }

        public static long? ObjToNullLong(this object thisValue, object errorValue)
        {
            if (thisValue != null && thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out long result))
            {
                return result;
            }
            else
            {
                return ObjToNullLong(errorValue);
            }
        }

        public static List<KeyValuePair<string, string>> ConvertJsonToList(string json)
        {
            // 将字符串转换为 JArray
            JArray jArray = JArray.Parse(json);

            // 创建一个 List<KeyValuePair<string, string>>
            List<KeyValuePair<string, string>> keyValuePairs = new List<KeyValuePair<string, string>>();

            // 遍历 JArray 中的每个 JObject
            foreach (JObject item in jArray)
            {
                if (item.ContainsKey("label") && item.ContainsKey("value"))
                {
                    string key = item["value"].ObjToString();
                    string value = item["label"].ObjToString();
                    keyValuePairs.Add(new KeyValuePair<string, string>(key, value));
                }
            }
            return keyValuePairs;
        }
    }
}
